//
//  LoginBottonView.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/4/25.
//

import UIKit

class LoginBottonView: UIView {

    typealias LoginBottonViewBlock = (_ type:LoginBottonType) -> Void
    var loginBottonViewBlock : LoginBottonViewBlock!

    var isSelect:Bool = false

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(self.countUSBtn)
        self.addSubview(self.agreementLab)
        self.countUSBtn.snp.makeConstraints { (make) in
            make.width.equalTo(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_Contact).getWidth(UIFont.systemFont(ofSize: 13))+25)
            make.centerX.equalTo(self.snp.centerX)
            make.bottom.equalTo(self.snp.bottom).offset(-10)
            make.height.equalTo(20)
        }
        self.agreementLab.snp.makeConstraints { (make) in
            make.bottom.equalTo(self.countUSBtn.snp.top).offset(-10)
            make.height.equalTo(40)
            make.left.equalTo(self.snp.left).offset(0)
            make.right.equalTo(self.snp.right).offset(0)
        }
        
        self.protocolIsSelect()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func clickCountUsBtn() {
        if (loginBottonViewBlock != nil) {
            loginBottonViewBlock(LoginBottonType.clickCountUs)
        }
    }
    
    func refreshUI(index:Int) {
        self.countUSBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_Contact), for: .normal)
        self.countUSBtn.snp.updateConstraints { (make) in
            make.width.equalTo(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_Contact).getWidth(UIFont.systemFont(ofSize: 13))+25)
        }
        self.protocolIsSelect()
    }
    
    func protocolIsSelect() {
        let str1:String = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.hint_agreeAndRead)
        let str2:String = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_serveAgreement)
        let str3 = str1 + str2
        let text = NSMutableAttributedString.init(string: str3)
        text.yy_color = UIColor.init(hexString: "#A3A3A3")
        text.yy_alignment = .center
        
        text.addAttribute(.underlineStyle, value: NSUnderlineStyle.single, range: NSRange.init(location: str1.count-1, length: str2.count))
        
        text.yy_setTextHighlight(NSRange.init(location: str1.count, length: str2.count), color: UIColor.init(hexString: "#4C4C4C"), backgroundColor: UIColor.clear) { (containerView, text, range, rect) in
            if (self.loginBottonViewBlock != nil) {
                self.loginBottonViewBlock(LoginBottonType.clickAgreement)
            }
        }
        var imageStr = "login_normail"
        if self.isSelect {
            imageStr = "login_select"
        }
        let imageIcon = UIImage.init(named: imageStr)
        let attachment:NSMutableAttributedString = NSMutableAttributedString.yy_attachmentString(withContent: imageIcon, contentMode: ContentMode.center, attachmentSize: CGSize.init(width: 25, height: 25), alignTo: UIFont.init(name: "PingFangSC-Regular", size: 12)!, alignment:YYTextVerticalAlignment.center)
        text.insert(attachment, at: 1)
        text.yy_setTextHighlight(NSRange.init(location: 0, length: 2/*attachment.string.count*/), color: UIColor.clear, backgroundColor: UIColor.clear) { (containerView, text, range, rect) in
            
            self.isSelect = !self.isSelect
            self.protocolIsSelect()
//            if (self.loginBottonViewBlock != nil) {
//                self.loginBottonViewBlock(LoginBottonType.clickSelectImg)
//            }
        }
        
        agreementLab.attributedText = text
    }
    
    lazy var countUSBtn:UIButton = {
        let countUSBtn = UIButton()
        countUSBtn.titleLabel?.textAlignment = .center
        countUSBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        countUSBtn.setTitle(ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_Contact), for: .normal)
        countUSBtn.setImage(UIImage.init(named: "login_phone"), for: .normal)
        countUSBtn.addTarget(self, action: #selector(clickCountUsBtn), for: .touchUpInside)
        countUSBtn.setTitleColor(UIColor.init(hexString: "#3A3A3A"), for: .normal)
        return countUSBtn
    }()
    
    lazy var agreementLab:YYLabel = {
        let agreementLab = YYLabel()
        agreementLab.font = UIFont.systemFont(ofSize: 13)
        agreementLab.textAlignment = .center
        agreementLab.preferredMaxLayoutWidth = Constants.screenWidth - 60
        agreementLab.numberOfLines = 0
        agreementLab.textVerticalAlignment = .center
        return agreementLab
    }()
}
